package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
)

type PaginationRequest struct {
	Order   string `json:"order"  valid:"order" form:"order"`
	Sort    string `json:"sort" valid:"sort" form:"sort"`
	PerPage int    `json:"per_page" valid:"per_page" form:"per_page"`
}

func Pagination(data interface{}, ctx *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"order": []string{
			"in:id,created_at,updated_at",
		},
		"sort": []string{
			"in:desc,asc",
		},
		"per_page": []string{
			"numeric_between:2,100",
		},
	}

	messages := govalidator.MapData{
		"order": []string{
			"in:排序字段只支持id,created_at,updated_at",
		},
		"sort": []string{
			"in:排序规则只支持asc,desc",
		},
		"per_page": []string{
			"numeric_between:每页条数介于2-100之间",
		},
	}
	return validate(data, rules, messages)
}
